package com.phpxiu.app.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.phpxiu.app.download.PicDownloadCallBack;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.custom.FrescoImageView;


/**
 * @author 吴金洲
 * @file
 * @brief 图片选择后预览
 * @note 图片选择后预览：包括剪切和未剪切的图片效果
 */
public class PicturePreview extends UIBase implements View.OnClickListener {
    public final static String TAG="PicturePreview";
    public final static String PREVIEW_ACTION="preview_action";
    public final static String PREVIEW_URL="preview_url";
    public final static String SAVE="save";
    public final static String UPLOAD="upload";
    public final static String SEND="send";

    private FrescoImageView preview;
    private Button operateBtn;
    private String action;
    private String url;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        action=getIntent().getStringExtra(PREVIEW_ACTION);
        url=getIntent().getStringExtra(PicturePreview.PREVIEW_URL);
        setContentView(R.layout.picture_preview);
        init();
    }

    final private void init(){
        if(getIntent().getData()==null){
            finish();
            return;
        }
        String title="";
        switch (action){
            case SAVE:
                title=getString(R.string.save_title);
                if(TextUtils.isEmpty(url)){
                    operateBtn.setEnabled(false);
                }
                break;
            case UPLOAD:
                title=getString(R.string.upload_title);
                break;
            case SEND:
                title=getString(R.string.send_title);
                break;
            default:
                break;
        }

        preview=(FrescoImageView)findViewById(R.id.preview_view);
        preview.setImageURI(getIntent().getData());
        operateBtn=(Button)findViewById(R.id.operate_btn);
        if(!TextUtils.isEmpty(title)){
            operateBtn.setText(title);
        }else{
            operateBtn.setEnabled(false);
        }
        findViewById(R.id.operate_btn).setOnClickListener(this);
        findViewById(R.id.exit_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.operate_btn:
                if(SAVE.equals(action)){
                    downloadAdnSave();
                    return;
                }else{
                    setResult(RESULT_OK);
                    finish();
                }
                break;
            case R.id.exit_btn:
                setResult(RESULT_CANCELED);
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 下载
     */
    public void downloadAdnSave(){
        if(SAVE.equals(action)&& TextUtils.isEmpty(url)){
            Toast.makeText(PicturePreview.this,"Url is invalid!", Toast.LENGTH_LONG).show();
            return;
        }
        OKHttp.downloadPic(url, new PicDownloadCallBack(KKYUtil.Str2MD5(url)) {
            @Override
            public void onErr() {
                Toast.makeText(PicturePreview.this,"Save Failed!",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onDownloadSuccess(Uri uri) {
                Toast.makeText(PicturePreview.this,"Saved!", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                intent.setData(uri);
                sendBroadcast(intent);
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }

}
